package gui2.g2;

import java.awt.GridBagConstraints;
import java.awt.Insets;
 
/**
 * 
 * Little helper class providing a more comfortable way to setup a widget's
 * layout when using GridBagLayout. All methods return the object itself, which
 * allows for the caller to concatenate several method calls in one line. The
 * last call should be done(), returning the GridBagConstraints object to use
 * for adding the component to its container.
 * 
 * Usage example:
 * 
 * getContentPane().add(myComponent, new GridBagHelp(0,0,2,1).fill().anchorLeft().scale(1,0).done());
 * 
 * 
 */
 
public class GridBagHelp {
 
    private final GridBagConstraints c = new GridBagConstraints();
 
    public GridBagHelp(int gridx, int gridy) {
        this(gridx, gridy, 1, 1);
    }
 
    public GridBagHelp(int gridx, int gridy, int gridwidth, int gridheight) {
        c.gridx = gridx;
        c.gridy = gridy;
        c.gridwidth = gridwidth;
        c.gridheight = gridheight;
    }
 
    public GridBagHelp hfill() {
        c.fill = GridBagConstraints.HORIZONTAL;
        return this;
    }
 
    public GridBagHelp vfill() {
        c.fill = GridBagConstraints.VERTICAL;
        return this;
    }
 
    public GridBagHelp fill() {
        c.fill = GridBagConstraints.BOTH;
        return this;
    }
 
    public GridBagHelp anchorLeft() {
        anchor(GridBagConstraints.WEST);
        return this;
    }
 
    public GridBagHelp anchorRight() {
        anchor(GridBagConstraints.EAST);
        return this;
    }
 
    public GridBagHelp anchor(int anchor) {
        c.anchor = anchor;
        return this;
    }
 
    public GridBagHelp scale(double weightx, double weighty) {
        c.weightx = weightx;
        c.weighty = weighty;
        return this;
    }
 
    public GridBagHelp bounds(int top, int left, int bottom, int right) {
        bounds(top, left, bottom, right, 0, 0);
        return this;
    }
 
    public GridBagHelp bounds(int top, int left, int bottom, int right,
            int ipadx, int ipady) {
        c.insets = new Insets(top, left, bottom, right);
        c.ipadx = ipadx;
        c.ipady = ipady;
        return this;
    }
 
    public GridBagConstraints done() {
        return c;
    }
}